using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NHibernate.Expression;
using Schooll.Model;

public partial class Secretaria_NotasDiciembreMarzo : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        cboCursos1.seleciona += new cboCurSelected(cboCursos1_seleciona);
        btnGuardar.Visible = false;
    }

    void cboCursos1_seleciona(string aselec)
    {
        FillGrid();
    }

    private void FillGrid()
    {

        if (cboCursos1.value != "")
        {
            SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ColegiosConnectionString"].ToString());
            SqlCommand cmd = new SqlCommand();
            string command = "select A.IdAlumno, A.Apellido + ',' + A.Nombre as nombre, NDM.Marzo from Alumnos A inner join NotasFinales NF on (A.IdAlumno = NF.IdAlumno and NF.IdMateria = " + cboCursos1.value + " and Marzo >0) left join NotasDiciembreMarzo NDM on (NDM.IdAlumno = A.IdAlumno and NDM.IdMateria = NF.IdMateria) order by A.Apellido";
            cmd.CommandText = command;
            cmd.CommandType = CommandType.Text;

            cmd.Connection = conn;

            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet _DataSet = new DataSet();
            da.Fill(_DataSet);
            conn.Close();

            GridView1.DataSource = _DataSet;
            GridView1.DataBind();
        }

        if (GridView1.Rows.Count > 0)
            btnGuardar.Visible = true;

    }

    protected void btnGuardar_Click(object sender, EventArgs e)
    {
        SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ColegiosConnectionString"].ToString());
        SqlCommand cmd = new SqlCommand();

        cmd.CommandType = CommandType.Text;

        cmd.Connection = conn;

        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            GridViewRow row = GridView1.Rows[i];
            string sNota = ((TextBox)row.FindControl("txtNotaDM")).Text;
            int IdAlumno = int.Parse(row.Cells[0].Text);
            int IdMateria = int.Parse(cboCursos1.value);
            int dNota;

            if (sNota == "")
            {
                NotasDiciembreMarzo ndm = Schooll.Model.NotasDiciembreMarzo.FindFirst(Expression.And(Expression.Eq("IdAlumno", IdAlumno), Expression.Eq("IdMateria", IdMateria)));
                if (ndm != null)
                {
                    ndm.Marzo =0;
                    ndm.SaveAndFlush();
                }
            }
            else
            {
                if (sNota.ToUpper() == "A")
                {
                    sNota = "99";
                }
                dNota = int.Parse(sNota);
                if ((dNota >= 0 && dNota <= 10)||dNota==99)
                {
                    NotasDiciembreMarzo ndm = Schooll.Model.NotasDiciembreMarzo.FindFirst(Expression.And(Expression.Eq("IdAlumno", IdAlumno), Expression.Eq("IdMateria", IdMateria)));
                    if (ndm == null)
                    {
                        ndm = new NotasDiciembreMarzo();
                        ndm.IdAlumno = IdAlumno;
                        ndm.IdMateria = IdMateria;
                        ndm.LastUpdate = System.DateTime.Now;
                        ndm.Anio = biFactory.Per.Anio;
                    }
                    ndm.Marzo = dNota;
                    ndm.SaveAndFlush();
                }
            }
        }
        FillGrid();
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ColegiosConnectionString"].ToString());
            SqlCommand cmd = new SqlCommand();
            string command = "select * from NotasDiciembreMarzo where IdAlumno = " + e.Row.Cells[0].Text + " and IdMateria = " + cboCursos1.value;
            cmd.CommandText = command;
            cmd.CommandType = CommandType.Text;

            cmd.Connection = conn;

            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet _DataSet = new DataSet();
            da.Fill(_DataSet);
            conn.Close();

            if (_DataSet.Tables[0].Rows.Count > 0)
            {
                if (_DataSet.Tables[0].Rows[0]["Marzo"].ToString()!="" && int.Parse(_DataSet.Tables[0].Rows[0]["Marzo"].ToString()) > 0 && int.Parse(_DataSet.Tables[0].Rows[0]["Marzo"].ToString())<11)
                {
                    ((TextBox)e.Row.FindControl("txtNotaDM")).Text = _DataSet.Tables[0].Rows[0]["Marzo"].ToString();
                }
                else
                {
                    if (_DataSet.Tables[0].Rows[0]["Marzo"].ToString() == "99")
                    {
                        ((TextBox)e.Row.FindControl("txtNotaDM")).Text = "A";
                    }
                    else
                    {
                        ((TextBox)e.Row.FindControl("txtNotaDM")).Text = "";
                    }
                    

                }
            }
        }
    }
}
